import { createStore } from 'vuex'
import router from '../router/index'
const store = createStore({
    state(){
      return {
        name:'123123',
        routerList:[{
          name:'首页',
          path:'/'
      }], //路由集合
        isshowMenus:'' //当前路由
      }
    },
    getters:{
      countArr(state){
         state.name = '111';
         return state.name;
      },
      countArr1(state){
        state.name = '222';
        return state.name;
      },
      countArr2(state){
            state.name = '333';
            return state.name;
      }
    },
    mutations:{
      setName(state,name){
        state.name = '111';
      },
      setName1(state,name){
        state.name = '222';
      },
      setName2(state,name){
        state.name = '333';
      },
      addMenuToRouter(state,[url,namees]){
        router.push(url)
        //将数据存储到localStorage里面去
        let isshowes = false;
        state.isshowMenus = url
        state.routerList.forEach((item,index) => {
            if(item.path === url){
              isshowes = true
            }
        })
        if(isshowes){
          router.push(url)
        }else{
          state.routerList.push({
              name:namees,
              path:url
          })
          router.push(url)
          localStorage.setItem('stateInfo',JSON.stringify(state.routerList))
        }
      },
      xx(state,name){
        console.log(name);
          if(name == '/'){
            alert('首页不能删除');
            return
        }
        state.routerList = state.routerList.filter((item,indexes) => {
            return item.path != name
        })
        localStorage.setItem('stateInfo',JSON.stringify(state.routerList))
        let b = state.routerList[state.routerList.length-1]
        setTimeout(()=>{
          state.isshowMenus = b.path
          router.push(b.path)
        },200)
      }
    }
})


export default store;